package leetcode.秋招.shopee;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author ：zsy
 * @date ：Created 2021/7/19 15:55
 * @description：
 */
public class 发送指令跟随坐标移动 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.GetEndPoint("22WS2D2W2D3S3A")));
    }

    public class Solution {
        /**
         * Note: 类名、方法名、参数名已经指定，请勿修改
         *
         * @param order string字符串
         * @return int整型一维数组
         */
        public int[] GetEndPoint(String order) {
            // write code here
            int x = 0;
            int y = 0;
            String[] dirs = {"A", "W", "S", "D"};
            while (order.length() != 0) {
                int index = Integer.MAX_VALUE;
                for (int i = 0; i < dirs.length; i++) {
                    if (order.indexOf(dirs[i]) != -1)
                        index = Math.min(order.indexOf(dirs[i]), index);
                }
                    if (index != -1 && index != Integer.MAX_VALUE) {
                        char c = order.charAt(index);
                        int num = 1;
                        if (index > 0) {
                            num = Integer.parseInt(order.substring(0, index));
                        }
                        if (c == 'A') {
                            x -= num;
                        } else if (c == 'W') {
                            y += num;
                        } else if (c == 'S') {
                            y -= num;
                        } else if (c == 'D') {
                            x += num;
                        }
                        order = order.substring(index + 1);
                }
            }
            return new int[]{x, y};
        }
    }
}
